package com.mdx.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mdx.bean.UserBean;
import com.mdx.service.IUserService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 用户
 */
@RestController
@RequestMapping("user")
public class UserController {
    @Resource
    private IUserService userService;

    /**
     * 登录页面
     * @param username
     * @param pwd
     * @return
     */
    @RequestMapping("login")
    public UserBean login(String username, String pwd){
        UserBean userBean = userService.login(username,pwd);
        if(userBean != null){
            return userBean;
        }
        return userBean;
    }

    /**
     * 展示所有用户
     * @param pageNO
     * @param pageSize
     * @return
     */
    @RequestMapping("findAll")
    public IPage<UserBean> findAll(@RequestParam(value = "pageNO",defaultValue = "1") int pageNO,
                                       @RequestParam(value = "pageSize",defaultValue = "4")int pageSize){
        return userService.findByUserType(pageNO,pageSize,null);
    }

    /**
     * 添加用户
     * @param userBean
     * @return
     */
    @RequestMapping("add")
    public String add(UserBean userBean){
        userService.save(userBean);
        return "ok";
    }

    /**
     * 删除用户
     * @param userId
     * @return
     */
    @RequestMapping("delUser")
    public String delUser(int userId){
        userService.removeById(userId);
        return "ok";
    }

    /**
     * 修改用户
     * @param userId
     * @param pwd
     * @param power
     * @return
     */
    @RequestMapping("updateUser")
    public String updateUser(int userId,String pwd,String power){
        userService.updateById(userId,pwd,power);
        return "ok";
    }

    /**
     * 用户模糊查询
     * @param pageNO
     * @param pageSize
     * @param power
     * @return
     */
    @RequestMapping("/findByPower")
    public IPage<UserBean> findGrade(@RequestParam(value = "pageNo", defaultValue = "1") int pageNO,
                                     @RequestParam(value = "pageSize", defaultValue = "4") int pageSize,
                                     String power) {
        return userService.findByUserType(pageNO,pageSize,power);
    }


}
